window.onload=function(){
    layui.use(['form','jquery','table','layer'],function(){
        var $ = jQuery = layui.$,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        // 表单
        form.on('select(one)',function(data){
            var id=data.value;
            $('.twoclass').html('');
            $('.threeclass').html('<option value="0">请选择三级分类</option>');
            if(id!=0){
                $.ajax({
                    'url': '/goods/get-tree',
                    'type': 'POST',
                    'dataType': 'json',
                    'data': {id:id},
                    'success': function (data) {
                        if (data.code != 200) {
                            layer.alert(data.msg);
                            return false;
                        } else {
                            var option = '<option value="0">请选择二级分类</option>';
                            $.each(data.data,function(indexe,value){
                                option += '<option value="'+value.id+'">'+value.tree_name+'</option>'
                            });
                            $('.twoclass').append(option);
                            form.render('select')
                        }
                    }
                });
            }else{
                form.render('select')
            }
        })
        form.on('select(two)',function(data){
            var id=data.value;
            $('.threeclass').html('');
            if(id!=0){
                $.ajax({
                    'url': '/goods/get-tree',
                    'type': 'POST',
                    'dataType': 'json',
                    'data': {id:id},
                    'success': function (data) {
                        if (data.code != 200) {
                            layer.alert(data.msg);
                            return false;
                        } else {
                            var option = '<option value="0">请选择三级分类</option>';
                            $.each(data.data,function(indexe,value){
                                option += '<option value="'+value.id+'">'+value.tree_name+'</option>'
                            });
                            $('.threeclass').append(option);
                            form.render('select')
                        }
                    }
                });
            }else{
                form.render('select');
            }
        })
        // 表格
        var config={
            elem:"#list",
            url:'/goods/index',
            cols:[[
                {type:'numbers', width:80, title: '编号'},
                {field:'goods_name', width:275, title: '商品'},
                {field:'org_pre', width:125, title: '价格'},
                {field:'shop_name', width:150, title: '商家'},
                {field:'sales_volume', width:120, title: '总销量'},
                {field:'', width:88, title: 'sku',toolbar: '#lookup'},
                {field:'status', width:100, title: '商品状态',templet: function (item) {
                    switch (parseInt(item.status)){
                        case 0:
                            return '下架';
                        case 1:
                            return '上架';
                    }
                }},
                {fixed:'right', width:150, align:'center', toolbar: '#operation',title:'操作'}
            ]],
            page:true,
        }
        table.render(config)
        table.on('tool(operation)',function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if(layEvent === 'detail'){
                location.href = '/goods/get-goodsinfo?id='+data.id;
            }else if(layEvent === 'lookup'){
                var skuTable='';
                $.ajax({
                    url:"/goods/get-goodsattr",
                    type:"get",
                    data:{id:data.id},
                    dataType:"json",
                    async:false,
                    success:function(res){
                        skuTable = '<table class="layui-table" style="max-height:375px;">'
                        skuTable +=     '<colgroup>';

                        $(res.data.thead).each(function(i,item){
                            skuTable += '<col width="120"></col>'
                        })
                                
                        skuTable +=         '<col width="120">'
                        skuTable +=         '<col width="120">'
                        skuTable +=         '<col width="120">'
                        skuTable +=         '<col width="120">'
                        skuTable +=         '<col width="120">'
                        skuTable +=     '</colgroup>'
                        skuTable +=     '<thead><tr>'
                        $(res.data.thead).each(function(i,item){
                            skuTable += '<th>'+item+'</th>'
                        })
                        skuTable +=         '<th>原价</th><th>折扣价</th><th>拼团库存</th>'
                        skuTable +=     '</tr></thead>'
                        skuTable +=     '<tbody>'
                        $(res.data.tbody).each(function(i,item){
                            skuTable += '<tr>'
                            $(item.val).each(function(i,item){
                                skuTable += '<th>'+item.value+'</th>'
                            })
                            skuTable +=         '<td>'+item.original_price+'</td>'
                            skuTable +=         '<td>'+item.discount_price+'</td>'
                            skuTable +=         '<td>'+item.inventory+'</td>'
                            skuTable += '</tr>'
                        })
                        skuTable +=     '</tbody>'
                        skuTable += '</table>'
                    }
                })
                Utils.layer.open({
                    type:1,
                    title:"SKU",
                    area:['800px','auto'],
                    content:skuTable
                })
            }
        })
        form.on("submit(search)",function(data){
            var goods_name = ($("input[name='goods_name']").val()) ? $("input[name='goods_name']").val() : '';
            var one_id = ($('.oneclass option:selected').val()) ? $('.oneclass option:selected').val() : '';
            var two_id = ($('.twoclass option:selected').val()) ? $('.twoclass option:selected').val() : '';
            var three_id = ($('.threeclass option:selected').val()) ? $('.threeclass option:selected').val() : '';
            var url = '/goods/index?goods_name='+ goods_name +'&one_id='+ one_id+'&two_id='+two_id+'&three_id='+three_id;
            config.url = url;
            table.render(config);
            return false;
        });
    })
}
